package com.hyb.controller;

import com.hyb.pojo.Role;
import com.hyb.pojo.User;
import com.hyb.service.RoleService;
import com.hyb.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "/admin/login";
    }
    @RequestMapping("/login")
    public String login(@RequestParam Map maps,HttpSession session,ModelMap map){
        //判断是否被禁用
        User user = userService.getByName((String) maps.get("username"));
        if (user.getStatus()==0){
            map.addAttribute("msg","账号已被禁用");
            return "/admin/login";
        }
        //获取当前用户
        Subject subject= SecurityUtils.getSubject();
        //封装用户的登录数据
        UsernamePasswordToken token = new UsernamePasswordToken((String)maps.get("username"),(String)maps.get("password"));
        try{
            subject.login(token);
            session.setAttribute("username",token.getUsername());
            return "/admin/index";
        }catch(UnknownAccountException e){
            map.addAttribute("msg","用户名错误");
            return "/admin/login";
        }catch(IncorrectCredentialsException e){
            map.addAttribute("msg","密码错误");
            return "/admin/login";
        }

    }
        /*List<User> all = userService.getAll(maps);
        for (User user : all) {
            if (user.getUsername().equals(maps.get("username"))){
                if(user.getPwd().equals(maps.get("password"))){
                    if(user.getStatus()==1){
                        session.setAttribute("user",user);
                        map.put("user",user);
                        return "/admin/index";
                    }else{
                        map.put("msg", "账号已被禁用");
                        return "/admin/login";
                    }
                }else{
                    map.put("msg","账号或密码错误");
                    return "/admin/login";
                }
            }
        }
        map.put("msg","账户不存在");
        return "/admin/login";
    }*/
    @RequestMapping("/toIndex")
    public String toIndex(){
        return "/admin/index";
    }
    @RequestMapping("/addUser")
    @ResponseBody
    public String addUser(@RequestParam Map data,Map map){
        String msg="";
        map.put("i_id",1);
        Date date1 = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String create_time = sf.format(date1);
        data.put("create_time",create_time);
        List<User> all = userService.getAll(map);
        for (User user : all) {
            if(user.getUsername().equals(data.get("username"))){
                msg="用户名已存在";
                return msg;
            }
        }
        msg= "ok";
        userService.addUser(data);
        return msg;
    }
    @RequestMapping("/delUser")
    public String delUser(@RequestParam Integer id) {
        userService.deleteById(id);
        return "redirect:/admin-list";
    }
    @RequestMapping("/toAdmin-edit")
    public String toAdminEdit(@RequestParam Integer id,ModelMap map){
        List<Role> roleList = roleService.getAll();
        User user = userService.getById(id);
        map.addAttribute("user",user);
        map.addAttribute("roleList",roleList);
        return "/admin/admin-edit";
    }
    @RequestMapping("/admin-edit")
    @ResponseBody
    public String adminEdit(User user){
        System.out.println(user);
        String msg= "";
        List<User> all = userService.getAllExcludeById(user.getU_id());
        for (User user1 : all) {
            if(user1.getUsername().equals(user.getUsername())){
                msg="用户名已存在";
                return msg;
            }
        }
        msg="ok";
        userService.updataUser(user);
        return msg;
    }
    @RequestMapping("/tomember-password")
    public String toMemberPassword(@RequestParam Integer id,ModelMap map) {
        User user = userService.getById(id);
        map.addAttribute("user", user);
        return "/admin/member-password";
    }
    @RequestMapping("/repassword")
    public String rePassword(@RequestParam Map user){
        User byName = userService.getByName((String)user.get("username"));
        byName.setPwd((String)user.get("newpassword"));
        userService.updataUser(byName);
        return "redirect:/admin-list";
    }
    @RequestMapping("/status")
    public String updataStatus(@RequestParam Integer id){
        User user = userService.getById(id);
        if (user.getStatus()==1){
            user.setStatus(0);
            userService.updataUser(user);
        }else {
            user.setStatus(1);
            userService.updataUser(user);
        }
        return "redirect:/admin-list";
    }
    @RequestMapping("/delSelectUser")
    public String delSelectUser(@RequestParam String[] data){
        for (int i=0;i<data.length;i++){
            userService.deleteById(Integer.parseInt(data[i]));
        }
        return"redirect:/admin-list";
    }

}
